﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {

    var departments = JSON.parse(document.getElementById('t'));

    var dataSource = new kendo.data.DataSource({
        schema: {
            data: function (response) {
                //toDateTime(response);
                return response.Datas || response.Data;
            },
            total: function (response) {
                return response.Total;
            },
            model: {
                id: "Id",
                fields: {
                    Id: { nullable: false },
                    IsValid: { type: "boolean", defaultValue: true },
                    Number: { validation: { required: true } },
                    DepartureTime: { validation: { required: true }, defaultValue: new Date() },
                    FlightTime: { validation: { required: true }, defaultValue: new Date() },
                    Type: { validation: { required: true }, defaultValue: "N" }
                }
            }
        },
        pageSize: 10,
        serverPaging: true,
        batch: true,
        transport: {
            read: {
                url: "/Flight/Index",
                ataType: "json",
                method: "get",
                //data: function () {
                //    //var name = document.getElementById('name').value;
                //    var select = $("#name").data("kendoComboBox");
                //    var name = select ? select.value() : null;
                //    return { name: name };
                //}
            },
            update: {
                url: "/flight/Update",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            destroy: {
                url: "/flight/Remove",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            create: {
                url: "/flight/Create",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return JSON.stringify({ flight: options.models });
                }
                if (operation == "read") {
                    return { page: options.page, pages: options.pageSize };
                }
            }
        }
    });

    $("#grid").kendoGrid({
        dataSource: dataSource,
        filterable: true,
        toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }, { name: "cancel", text: '撤销修改' }],
        pageable: {
            refresh: true,
            pageSizes: true,
            buttonCount: 5
        },
        columns: [
            {
                field: "Number", title: "航班号", width: '180px',
                filterable: {
                    extra: false,
                    operators: {
                        string: {
                            contains: "包含",
                        }
                    }
                },
            },
        {
            field: "DepartureTime", title: "航班日期",
            template: function (value) {
                return moment(value.DepartureTime || value).format('YYYY-MM-DD')
            },
            width: '160px',
            editor: function (container, options) {
                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                .kendoAutoComplete({
                    placeholder: "顶级部门",
                    dataTextField: "Name",
                    dataSource: departments
                });
            }

        },
        {
            field: "FlightTime",
            title: "起飞时间",
            template: function (value) {
                return moment(value.FlightTime || value).format('HH:mm')
            },
            width: '250px',
            editor: function (container, options) {
                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                .kendoTimePicker({
                    format: 'HH:mm',
                    interval: 1
                });
            },
            filterable: false,

        },
        {
            template: '<input type="checkbox" #= IsValid ? \'checked="checked"\' : "" # class="chkbx" />',
            field: "IsValid",
            title: "是否有效",
            width: '100px',
            filterable: false,

        },
        {
            field: "Type",
            title: "航班类型",
            template: function (value) {
                if (value.Type == "N") return "正班";
                if (value.Type == "J") return "补班";
                if (value.Type == "B") return "包机";
                return value.Type;
            },
            width: '180px',
            editor: function (container, options) {
                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                .kendoDropDownList({
                    format: 'yyyy-MM-dd',
                    dataSource: {
                        data: [{
                            text: "正班",
                            value: 'N'
                        }, {
                            text: "补班",
                            value: 'J'
                        }, {
                            text: "包机",
                            value: 'B'
                        }]
                    },
                    dataTextField: "text",
                    dataValueField: "value",
                });
            },
            filterable: false,

        },
        {
            command: [
              { name: "destroy", 'text': '删除' },
              { name: "edit", text: '编辑' }
            ]
        }
        ],
        editable: { //disables the deletion functionality
            update: true,
            destroy: true,
            create: true,
            mode: 'popup',// inline popup
        },
        //editable: "popup",
        pageable: true,

    });

    $("#grid .k-grid-content").on("change", "input.chkbx", function (e) {
        var grid = $("#grid").data("kendoGrid");
        var dataItem = grid.dataItem($(e.target).closest("tr"));

        dataItem.set("IsValid", this.checked);
    });
});